package com.hyxf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Validator;
import com.hyxf.domain.cluster.ClusterUserInfo;
import com.hyxf.domain.cluster.ClusterUserClient;
import com.hyxf.service.*;
import com.hyxf.vo.ClusterUserInfoVO;
import com.hyxf.vo.MasterUserInfoVO;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 阿紫
 * @version 1.0.0
 * @project sync-multiple-datasource
 * @description 同步数据
 * @date 2024/11/08 14:22:58
 */
@Service
public class SyncDataSourceServiceImpl implements SyncDataSourceService {

    @Resource
    private ClusterUserInfoService clusterUserInfoService;

    @Resource
    private MasterUserInfoService masterUserInfoService;

    @Resource
    private MasterUserClientService masterUserClientService;

    @Resource
    private ClusterUserClientService clusterUserClientService;

    @Override
    @Scheduled(cron = "${cron.daily-execution}")
    public void syncUserInfo() {
        List<MasterUserInfoVO> masterUserInfoVOS = masterUserInfoService.findMasterUserInfoVOS();
        List<ClusterUserInfoVO> clusterUserInfoVOS = clusterUserInfoService.findClusterUserInfoVOS();
        for (MasterUserInfoVO masterUserInfoVO : masterUserInfoVOS) {
            Long masterqcId = masterUserInfoVO.getQcId();
            // 注册来源
            String registerSource = masterUserInfoVO.getRegisterSource();
            if (Validator.isNotNull(masterqcId)) {
                List<ClusterUserInfoVO> alreadyAccountVOS = clusterUserInfoVOS.stream().filter(e->masterqcId.equals(e.getQcId())&&registerSource.equals(e.getRegisterSource())).collect(Collectors.toList());
                // 验证qcId账号
                handleHasQcIdAccount(masterUserInfoVO, masterqcId, registerSource, alreadyAccountVOS, clusterUserInfoVOS);
            } else {
                // qcId没有的情况,传null
                handleNoAuthQcId(clusterUserInfoVOS, masterUserInfoVO, registerSource, null);
            }


        }
    }

    /**
     * 查询有qcId的账号
     * @param masterUserInfoVO
     * @param masterqcId
     * @param registerSource
     * @param alreadyAccountVOS
     * @param clusterUserInfoVOS
     */
    private void handleHasQcIdAccount(MasterUserInfoVO masterUserInfoVO, Long masterqcId, String registerSource, List<ClusterUserInfoVO> alreadyAccountVOS, List<ClusterUserInfoVO> clusterUserInfoVOS) {
        if (CollUtil.isNotEmpty(alreadyAccountVOS)) {
            ClusterUserInfoVO clusterAccountVO = alreadyAccountVOS.get(0);
            Long id = clusterAccountVO.getId();
            ClusterUserInfo clusterUserInfo = new ClusterUserInfo();
            BeanUtil.copyProperties(masterUserInfoVO, clusterUserInfo);
            clusterUserInfo.setId(id);
            clusterUserInfoService.updateById(clusterUserInfo);
        } else {
            // qcId没有的情况
            handleNoAuthQcId(clusterUserInfoVOS, masterUserInfoVO, registerSource, masterqcId);
        }
    }

    /**
     * 没有qcId的情况
     * @param clusterUserInfoVOS 从库用户列表
     * @param masterUserInfoVO 主库用户
     * @param registerSource 注册来源
     * @param masterqcId 用户qcId
     */
    private void handleNoAuthQcId(List<ClusterUserInfoVO> clusterUserInfoVOS, MasterUserInfoVO masterUserInfoVO, String registerSource, Long masterqcId) {
        // qcId为Null情况
        // 文曲星
        if (registerSource.equals("1")) {
             // 设备号、手机号
             String imei = masterUserInfoVO.getImei();
             String phone = masterUserInfoVO.getPhone();
             List<ClusterUserInfoVO> alreadyAccountVOS = clusterUserInfoVOS.stream().filter(e->imei.equals(e.getImei())&&phone.equals(e.getPhone())&&registerSource.equals(e.getRegisterSource())).collect(Collectors.toList());
             handleClusterUserInfo(masterUserInfoVO, registerSource, alreadyAccountVOS, masterqcId);
        }
        // PC端
        else if (registerSource.equals("2")) {
            String phone = masterUserInfoVO.getPhone();
            List<ClusterUserInfoVO> alreadyAccountVOS = clusterUserInfoVOS.stream().filter(e->phone.equals(e.getPhone())&& registerSource.equals(e.getRegisterSource())).collect(Collectors.toList());
            handleClusterUserInfo(masterUserInfoVO, registerSource, alreadyAccountVOS, masterqcId);
        }
        // WEB端
        else if(registerSource.equals("3")) {
            String phone = masterUserInfoVO.getPhone();
            String openid = masterUserInfoVO.getOpenid();
            List<ClusterUserInfoVO> alreadyAccountVOS = new ArrayList<>();
            if (Validator.isNotEmpty(phone)&&Validator.isNotEmpty(openid)) {
                alreadyAccountVOS = clusterUserInfoVOS.stream().filter(e->phone.equals(e.getPhone())&&openid.equals(e.getOpenid())&& registerSource.equals(e.getRegisterSource())).collect(Collectors.toList());
            }
            handleClusterUserInfo(masterUserInfoVO, registerSource, alreadyAccountVOS, masterqcId);
        }
        // 微信小程序端
        else if(registerSource.equals("4")) {
            String phone = masterUserInfoVO.getPhone();
            String name = masterUserInfoVO.getName();
            List<ClusterUserInfoVO> alreadyAccountVOS = new ArrayList<>();
            if (Validator.isNotEmpty(phone)&&Validator.isNotEmpty(name)) {
                alreadyAccountVOS = clusterUserInfoVOS.stream().filter(e->phone.equals(e.getPhone())&&name.equals(e.getName())).collect(Collectors.toList());
            }
            handleClusterUserInfo(masterUserInfoVO, registerSource, alreadyAccountVOS, masterqcId);
        }
        // 阿紫传奇端
        else if(registerSource.equals("5")) {
            String phone = masterUserInfoVO.getPhone();
            String name = masterUserInfoVO.getName();
            List<ClusterUserInfoVO> alreadyAccountVOS = new ArrayList<>();
            if (Validator.isNotEmpty(phone)&&Validator.isNotEmpty(name)) {
                alreadyAccountVOS = clusterUserInfoVOS.stream().filter(e->phone.equals(e.getPhone())&&name.equals(e.getName())).collect(Collectors.toList());
            }
            handleClusterUserInfo(masterUserInfoVO, registerSource, alreadyAccountVOS, masterqcId);
        }
    }

    /**
     * 处理qcId为空情形的账号信息
     * @param masterUserInfoVO
     * @param registerSource
     * @param alreadyAccountVOS
     */
    private void handleClusterUserInfo(MasterUserInfoVO masterUserInfoVO, String registerSource, List<ClusterUserInfoVO> alreadyAccountVOS, Long masterqcId) {
        if (CollUtil.isNotEmpty(alreadyAccountVOS)) {
            ClusterUserInfoVO clusterAccountVO = alreadyAccountVOS.get(0);
            Long id = clusterAccountVO.getId();
            ClusterUserInfo clusterUserInfo = new ClusterUserInfo();
            BeanUtil.copyProperties(masterUserInfoVO, clusterUserInfo);
            clusterUserInfo.setId(id);
            clusterUserInfoService.updateById(clusterUserInfo);
        }  else {
            ClusterUserInfo clusterUserInfo = new ClusterUserInfo();
            BeanUtil.copyProperties(masterUserInfoVO, clusterUserInfo);
            clusterUserInfo.setId(null);
            clusterUserInfoService.save(clusterUserInfo);
            Long userInfoId = clusterUserInfo.getId();
            ClusterUserClient clusterUserClient = new ClusterUserClient();
            clusterUserClient.setTerminalSource(registerSource);
            clusterUserClient.setState(0);
            clusterUserClient.setUserInfoId(userInfoId);
            clusterUserClient.setQcId(masterqcId);
            clusterUserClientService.save(clusterUserClient);
        }
    }
}
